
var condition;//筛选条件
$(function(){
    var href=decodeURI(window.location.href);
    var params=[];
    if(href.indexOf("?")>0){
        params=href.substr(href.indexOf("?")+1).split("&");
    }
    condition=new Condition();
    for(var i in params){
        var param=params[i].split("=");
        switch(param[0]){
            case "tagId":
                condition.setTagId(param[1]);
                break;
            case "typeId":
                condition.setTypeId(param[1]);
                break;
            case "tagIds":
                condition.addToTagList(param[1]);
                break;
            case "includeAllTags":
                condition.setIncludeAllTags(param[1]);
                break;
            case "keywords":
                condition.setKeywords(param[1]);
                break;
        }
    }
    loadFilterArticle(1,condition);
    loadBreadcrumbs(condition);
});
var Condition=function(){
    this.tagList=[];
    this.tagId='';
    this.typeId='';
    this.includeAllTags='';
    this.keywords='';
    this.setTagId=function(tagId){
        if(isNaN(tagId))return;
        this.tagId=parseInt(tagId);
    }
    this.setTypeId=function(typeId){
        if(isNaN(typeId))return;
        this.typeId=parseInt(typeId);
    }
    this.addToTagList=function(tagId){
        if(isNaN(tagId))return;
        this.tagList.push(parseInt(tagId));
    }
    this.setIncludeAllTags=function (boolean){
        if(boolean=='true'){
            this.includeAllTags=true;
        }else{
            this.includeAllTags=false;
        }
    }
    this.setKeywords=function (keywords){
        this.keywords=keywords;
    }
}

function loadBreadcrumbs(condition){
    var breadcrumbsHtml='';
    if(condition.typeId!=''){
        $.ajax({
            url:'/api/type/getType/'+condition.typeId,
            type:'post',
            async:false,
            success:function(resp){
                if(resp.status==200){
                    var type=resp.data;
                    breadcrumbsHtml+='&nbsp;>&nbsp;' +
                        '            <a href="/list.html?typeId='+type.id+'" title="'+type.description+'">'
                        +type.name+'</a>';
                }
            }
        });
    }
    if(condition.tagId!=''||condition.tagList.length!=0){
        $.ajax({
            url:'/api/tags/getTags',
            type:'post',
            async:false,
            data:{
                tagIds:condition.tagId!=''?[condition.tagId]:condition.tagList
            },
            success:function(resp){
                if(resp.status==200){
                    breadcrumbsHtml+="&nbsp;>&nbsp;";
                    var tags=resp.data;
                    for( var i in tags){
                        var tag=tags[i];
                        breadcrumbsHtml+='<a href="/list.html?tagId='+tag.id+'" title="'+tag.description+'"><i class="fa fa-tag"></i>'+tag.name+'</a>';
                        if(i<tags.length-1){
                            breadcrumbsHtml+=condition.includeAllTags?"&":"|";
                        }
                    }
                }
            }
        });
    }

    if(condition.keywords!=null&&condition.keywords!=''){
        breadcrumbsHtml+='&nbsp;>&nbsp;搜索：' +
            '            <a href="javascript:;" title="">'+condition.keywords+'</a>';
    }
    $(".breadcrumbs").append(breadcrumbsHtml);
}

function loadFilterArticle(pageNumber,condition){
    $.ajax({
        url:'/api/article/list',
        type:'post',
        data:$.extend({
            pageNumber:pageNumber,
            pageSize:10,
        },condition),
        success:function(resp){
            $(".blog-main").html("");
            $(".blog-main").data("totalPage",resp.totalPage);
            for(var i in resp.rows){
                $(".blog-main").append(getArticleHtml(resp.rows[i]));
            }
            var pageLi='';
            var start=resp.pageNum-2;
            if(start<=0)start=1;
            for(var j=start;j<start+5&&j<=resp.totalPage;j++){
                if(j==resp.pageNum){
                    pageLi+='<li class="active"><a onclick="loadFilterArticle('+j+',condition)">'+j+'</a></li>';
                }else{
                    pageLi+='<li><a onclick="loadFilterArticle('+j+',condition)">'+j+'</a></li>';
                }
            }
            $(".blog-main").append('<div>' +
                '<ul class="pagination">' +
                '<li><a onclick="loadFilterArticle(1,condition)">首页</a></li>' +
                pageLi
                +'<li><a onclick="loadFilterArticle('+resp.totalPage+',condition)">末页</a></li>'
                +'<li><a>共'+resp.totalPage+'页</a></li>' +
                '<li class="col-sm-3"><input name="pageNumber" class="form-control pull-left" type="text" placeholder="跳转页码"></li>' +
                '<li><button  onclick="jumpPage()" class="btn btn-default"><i class="fa fa-mail-forward"></i></button></li>'+
                '</ul></div>');

            showAllCommentCount();
        }
    });
}
function jumpPage(){
    var pageNumer=$("input[name='pageNumber']").val();
    if(isNaN(pageNumer)||pageNumer<1||pageNumer>$(".blog-main").data("totalPage")){
        toastr.error("请输入正确页码！");
        return;
    }
    loadFilterArticle(pageNumer,condition);
}

function getArticleHtml(article){
    tagsHtml='';
    for(var i in article.tags){
        var tag=article.tags[i];
        tagsHtml+='<a class="tag-'+tag.id%5+'" href="/list.html?tagId='+tag.id+'" title="'+tag.description+'">\n' +
            '                <i class="fa fa-tag"></i>'+tag.name+'</a>';
    }
    return '<article class="fade-in">' +
        '<figure class="thumbnail">' +
        '<a href="/article/'+article.id+'.html">' +
        '' +
        '<img width="150" height="150" src="'+article.coverImage+'" class="img-responsive lazy-img" alt="封面" style="width: 150px;height: 130px;"></a>' +
        '<span class="cat a-type"><a href="/list.html?typeId='+article.type.id+'">'+article.type.name+'</a></span>' +
        (article.top?'<span class="cat top" style="top:30px"><a>置顶</a></span>':'')+
        (article.recommended?'<span class="cat recommended" style="top:'+(article.top?'60':'30')+'px"><a>推荐</a></span>':'')+
        '</figure>' +
        '<header class="entry-header">' +
        '<h2 class="entry-title">' +
        '<a href="/article/'+article.id+'.html" rel="bookmark" title="点击查看文章详情" data-toggle="tooltip" data-placement="bottom">'+article.title+'</a>' +
        '</h2>' +
        '</header>' +
        '<div class="entry-content">' +
        '<div class="archive-content">' +
        article.description +
        '</div>' +
        '<span class="title-l"></span>' +
        '<div class="entry-meta">' +
        '<span class="date" title="文章发表日期" data-toggle="tooltip" data-placement="bottom"><i class="fa fa-calendar-o fa-fw"></i>'+article.createTime+'</span>\n' +
        '<span class="views" title="文章阅读次数" data-toggle="tooltip" data-placement="bottom"><i class="fa fa-eye fa-fw"></i>浏览('+article.lookCount+')</span>' +
        '<span class="loves" title="获赞次数" data-toggle="tooltip" data-placement="bottom"><i class="fa fa-thumbs-up fa-fw"></i>赞('+article.loveCount+')</span>' +
        '<span class="comment" title="文章评论次数" data-toggle="tooltip" data-placement="bottom">' +
        '<a href="/article/'+article.id+'.html#comment-box" rel="external nofollow">' +
        '<i class="fa fa-comments-o fa-fw"></i>评论(<span class="comment_count" data-id="'+article.id+'"></span>)' +
        '</a>' +
        '</span>' +
        '<span>' +
        tagsHtml+
        '</span>'+
        '</div>' +
        '<div class="clear"></div>' +
        '<span class="entry-more">' +
        '<a href="/article/'+article.id+'.html" rel="bookmark" title="点击查看文章详情" data-toggle="tooltip" data-placement="bottom">阅读全文</a>' +
        '</span>' +
        '</div>' +
        '</article>';
}
